package servlet;


import domain.Products;
import service.ProductService;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class FindProductAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, IOException {
        /**
         * 1.获得请求参数值
         * 2.调用业务逻辑
         * 3.获得结果
         * 4.根据结果判断是否需要共享对象(session/request)
         * 5.转发或重定向
         */
        //session共享

        HttpSession session = request.getSession();
        //共享
        int count;
        int pre;
        int post;
        ServletContext application = this.getServletContext();
        request.setCharacterEncoding("utf-8");
        if (application.getAttribute("count") == null) {
            application.setAttribute("count", new Integer(0));
        }
         count = (Integer) application.getAttribute("count");
        application
                .setAttribute("count", new Integer(count + 1));
        count = (Integer) application.getAttribute("count");
        if (application != null) {


            pre = count + 1;
            post = count + 3;

        }
        else {
            pre=0;
            post=1;
        }
        ProductService ps = new ProductService();
        List<Products> data = ps.findByAll(pre, post);

        session.setAttribute("productdata", data);
        response.sendRedirect("index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

